package com.smt.take.ui.adapter

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.smt.take.R
import com.smt.take.model.beans.GoodsListBean
import com.smt.take.ui.fragment.GoodsFragment
import kotlinx.android.synthetic.main.fragment_goods.*
import org.jetbrains.anko.find

class GoodsTypeRvAdapter(val context: Context?, val goodsFragment: GoodsFragment) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var typeList: MutableList<GoodsListBean> = mutableListOf()

    fun setData(typeList: MutableList<GoodsListBean>) {
        this.typeList = typeList
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val itemView = LayoutInflater.from(context).inflate(R.layout.item_type, parent, false)
        return GoodsViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as GoodsViewHolder).bindData(typeList[position], position)
    }

    override fun getItemCount(): Int {
        return typeList.size
    }

    var selectPosition = 0 // 选中的位置

    inner class GoodsViewHolder(val item: View) : RecyclerView.ViewHolder(item) {
        private val typeName = item.find<TextView>(R.id.type)
        private val tvRedDotCount = item.find<TextView>(R.id.tvRedDotCount)
        private var mPosition: Int = 0
        lateinit var goodsListBean: GoodsListBean

        init {
            item.setOnClickListener {
                selectPosition = mPosition
                notifyDataSetChanged()
                // 右侧列表跳转到该类型中第一个商品
                val typeId = goodsListBean.id
                // 遍历所有商品，找到此position
                val position = goodsFragment.presenter.getGoodsPositionByTypeId(typeId)
                goodsFragment.slhlv.setSelection(position)
            }
        }

        fun bindData(goodsListBean: GoodsListBean, position: Int) {
            mPosition = position
            this.goodsListBean = goodsListBean
            if (position == selectPosition) {
                // 选中的为白底加粗黑字
                item.setBackgroundColor(Color.WHITE)
                typeName.setTextColor(Color.BLACK)
                typeName.typeface = Typeface.DEFAULT_BOLD
            } else {
                // 未选中是灰色背景，普通字体
                item.setBackgroundColor(Color.parseColor("#b9dedcdc"))
                typeName.setTextColor(Color.GRAY)
                typeName.typeface = Typeface.DEFAULT
            }
            typeName.text = goodsListBean.name
            tvRedDotCount.text = goodsListBean.redDotCount.toString()
            if (goodsListBean.redDotCount > 0) {
                tvRedDotCount.visibility = View.VISIBLE
            } else {
                tvRedDotCount.visibility = View.GONE
            }
        }

    }

}